<?php
defined('IN_MAIN') or exit('Invalid Access!');

function output_compressed_file($files){
/*
this function will compress the input file(s) and store all the combine files into 1 compressed file.
the location of the output compressed file will be the same path as the last input file path
*/
 global $vars;

 $js_path=JS_ROOT;

 $r_file=array();
 $r_file=explode(",", $files);
 foreach($r_file as $file){
  $key.=$file;
  $temp_file="$js_path/$file";
  if(!$file || !file_exists($temp_file)){
   exit;
  }
  $r_path_file[]=$temp_file;
  $file_last_mod=($temp=filemtime($temp_file))>$file_last_mod? $temp : $file_last_mod;
 }
 //use the last file path as store path
 $input_path=substr($file, 0, ($len=strrpos($file, "/"))===false? 0 : $len);
 $js_store_path=JS_ROOT.($input_path? "/$input_path" : "");
 $js_store_path=JS_ROOT."/js_store";

 if($vars["js_gzip"] && !headers_sent()){//check compression
  $encodings=array();
  if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
   $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));

  if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
   $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
   $supportsGzip = true;
  }
 }
 if(function_exists(apache_request_headers)){
  $headers=apache_request_headers();
  $if_modified_since=$headers["If-Modified-Since"];
 }else{
  //make sure the .htaccess files have rules incorporated
  $if_modified_since=$_SERVER['HTTP_IF_MODIFIED_SINCE'];
 }
 if(isset($if_modified_since)){
  $modified_since=strtotime($if_modified_since);
 }

 $cache_file="$js_store_path/js_".md5($key).($supportsGzip? ".gz" : ".js");
 if(file_exists($cache_file)){//use cache file
  $cache_last_mod=filemtime($cache_file);
  if($cache_last_mod<$file_last_mod){//cache file is old, recreate
   $content=get_all_contents($r_path_file);
   $content=$supportsGzip? gzencode($content, 9, FORCE_GZIP) : $content;
   write_file($cache_file, $content);
  }else{
   if($cache_last_mod<=$modified_since){//user cache file is current
    header("HTTP/1.1 304 Not Modified");
    exit;
   }else{
    $content=file_get_contents($cache_file);
   }
  }
 }else{//create new cache file
  $content=get_all_contents($r_path_file);
  $content=$supportsGzip? gzencode($content, 9, FORCE_GZIP) : $content;
  write_file($cache_file, $content);
 }

 preg_match("/(?<=\.)[a-zA-Z0-9]{1,4}\$/", $files, $match);
 if($match[0]=="js"){
  $type="text/javascript";
 }elseif($match[0]=="css"){
  $type="text/css";
 }else{
  $type="text/html";
 }
 if(!headers_sent()){
  header("Content-Type:$type; charset=$vars[charset]");
  header("Vary: Accept-Encoding");  // Handle proxies
  header("Last-Modified: " . date("r", filemtime($cache_file)));
  header("Expires: " . date("r", time()-10000));
  if($supportsGzip){
   header('Content-Encoding: '.$enc);
  }
 }
 print $content;
}

function get_all_contents($files){
 for($i=0;$i<count($files);$i++){
  $c.=@file_get_contents($files[$i])."\n";
 }
 return rtrim($c, "\n");
}

function write_file($file, $content){
 if (function_exists("file_put_contents"))
  return @file_put_contents($file, $content);

 $fp=fopen($file, "wb");
 if($fp){
  fwrite($fp, $content);
  fclose($fp);
 }
}
?>